//
//  EarningsStatementTableViewController.swift
//  ZNZXSuperMarket
//
//  Created by 四川中农 on 2018/8/6.
//  Copyright © 2018年 四川中农. All rights reserved.
//

import UIKit
import HandyJSON
import Photos

class EarningsStatementTableViewController: BaseViewController {
    
    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var navView: UIView!
    @IBOutlet weak var navViewHeight: NSLayoutConstraint!
    
    var isScreenHot = false
    
    var currentModel:OrderStatisModel!
    var lastModel:OrderStatisModel!
    var headerLabel:UILabel!
    var type = 0 // 0 本月 1 本周
    
    var index = 1 // 1 标类 2 果蔬 3 生鲜
    
    var monthButton:UIButton!
    var weekButton:UIButton!
    
    var startTime = ""
    var endTime = ""
    
    @IBOutlet weak var rightBarButton: UIButton!
    
    lazy var effect:EffectView = {
        let view = EffectView.init(frame: CGRect.init(x: 0, y: Utils.isIphoneX() ? 88 : 64, width: UIScreen.ScreenWidth, height: UIScreen.ScreenHeight - (Utils.isIphoneX() ? 88 : 64)))
        view.alpha = 0
        self.view.addSubview(view)
        view.hidenClosure = { [weak self]  in
            UIView.animate(withDuration: 0.3, animations: {
                self?.filterView.height = 0
            }) { (_) in
                self?.effect.alpha = 0
                self?.rightBarButton.isSelected = false
            }
        }
        return view
    }()
    
    lazy var filterView:StatementFilterView = {
       let view = StatementFilterView.init(frame: CGRect.init(x: 0, y: Utils.isIphoneX() ? 88 : 64, width: UIScreen.ScreenWidth, height: 0), titleArray:["自定义统计","导出数据报表"])
        view.seleClosure = { [weak self] (value) in
            if value == "自定义统计" {
                let ctrl = Utils.instantiateInitialViewControllerWith(storyBoardName: "Main", identitier: "CustomStatisticsTableViewController") as! CustomStatisticsTableViewController
                self?.navigationController?.pushViewController(ctrl, animated: true)
                UIView.animate(withDuration: 0.3, animations: {
                    self?.filterView.height = 0
                }) { (_) in
                    self?.effect.alpha = 0
                    self?.rightBarButton.isSelected = false
                }
            }else{
                if self?.currentModel.count == 0 {
                    PKhud.showText(text: "您暂时还没有采购记录~")
                    return
                }
                self?.checkPhoto(authorizedBlock: {
                    self?.isScreenHot = true
                    self?.tableView.setContentOffset(CGPoint.init(x: 0, y: 0), animated: false)
                    Utils.DispatchAfter(after: 0.5, handler: {
                        self?.tableView.reloadData()
                    })
                    PKhud.showProgress(title: "正在生成报表~")
                    Utils.DispatchAfter(after: 1, handler: {
                        let image = Screenshot.getTableViewimage(self?.tableView)
                        UIImageWriteToSavedPhotosAlbum(image!, self, #selector(self?.image(image:didFinishSavingWithError:contextInfo:)), nil)
                    })
                }) {
                    PKhud.showText(text: "请在设置中允许访问相册~")
                    Utils.DispatchAfter(after: 2, handler: {
                        let url = URL.init(string: "App-Prefs:root=Privacy&path=Photos")
                        let url1 = URL.init(string: UIApplicationOpenSettingsURLString)
                        if #available(iOS 11, *) {
                            if UIApplication.shared.canOpenURL(url1!) {
                                UIApplication.shared.openURL(url1!)
                            }
                        }else{
                            if UIApplication.shared.canOpenURL(url!) {
                                if #available(iOS 10, *) {
                                    UIApplication.shared.open(url!, options:[:], completionHandler: nil)
                                }else{
                                    UIApplication.shared.openURL(url!)
                                }
                            }
                        }
                    })
                }
            }
        }
        view.clipsToBounds = true
        self.view.addSubview(view)
        return view
    }()
    
    
    
    
    @objc func image(image: UIImage, didFinishSavingWithError error: NSError?, contextInfo: AnyObject) {
        PKhud.hiden()
        
        if error != nil {
            print("错误")
            return
        }
        PKhud.showText(text: "报表生成成功，请在相册中查看~")
        self.isScreenHot = false
        self.tableView.reloadData()
        
        UIView.animate(withDuration: 0.3, animations: {
            self.filterView.height = 0
        }) { (_) in
            self.effect.alpha = 0
            self.rightBarButton.isSelected = false
        }
    }
    
    func checkPhoto(authorizedBlock: @escaping () -> (), deniedBlock:  @escaping () -> ()) {
        let authStatus = PHPhotoLibrary.authorizationStatus()
        
        // .notDetermined .authorized .restricted .denied
        if authStatus == .notDetermined {
            // 第一次触发授权 alert
            PHPhotoLibrary.requestAuthorization { (status:PHAuthorizationStatus) -> Void in
                self.checkPhoto(authorizedBlock: authorizedBlock, deniedBlock: deniedBlock)
            }
        } else if authStatus == .authorized {
            Utils.DispatchAfter(after: 0.5, handler: {
                authorizedBlock()
            })
        } else {
            Utils.DispatchAfter(after: 0.5, handler: {
                deniedBlock()
            })
            
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.initHeaderView()
        
        self.navViewHeight.constant = Utils.isIphoneX() ? 88 : 64
        self.tableView.backgroundColor = UIColor.backgroundColor
        
        let viewLine = UIView.init(frame: CGRect.init(x: 0, y: Utils.isIphoneX() ? 88-0.5 : 64-0.5, width: UIScreen.ScreenWidth, height: 0.5))
        viewLine.backgroundColor = UIColor.viewLineColor
        self.navView.addSubview(viewLine)
        
        let height = Utils.isIphoneX() ? 88 : 64
        let filterView = UIView.init(frame: CGRect.init(x: Int(UIScreen.ScreenWidth / 2 - 55), y: height - 7 - 30, width: 110, height: 30))
        filterView.setBorder(color: "#f7c201", width: 1)
        filterView.setCornerRadio(radio: 15, isMask: true)
        self.navView.addSubview(filterView)
        
        
        self.monthButton = UIButton.init(type: .custom)
        self.monthButton.frame = CGRect.init(x: 0, y: 0, width: 55, height: 30)
        self.monthButton.backgroundColor = UIColor.white
        self.monthButton.tag = 101
        self.monthButton.setTitle("本月", for: .normal)
        self.monthButton.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        self.monthButton.setTitleColor(UIColor.init(hexColor: "#ffffff"), for: .selected)
        self.monthButton.setTitleColor(UIColor.init(hexColor: "f7c201"), for: .normal)
        self.monthButton.addTarget(self, action: #selector(filterButtonClick(sender:)), for: .touchUpInside)
        filterView.addSubview(self.monthButton)
        
        self.weekButton = UIButton.init(type: .custom)
        self.weekButton.frame = CGRect.init(x: Int(filterView.width/2), y: 0, width: 55, height: 30)
        self.weekButton.backgroundColor = UIColor.white
        self.weekButton.tag = 102
        self.weekButton.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        self.weekButton.setTitle("本周", for: .normal)
        self.weekButton.setTitleColor(UIColor.init(hexColor: "#ffffff"), for: .selected)
        self.weekButton.setTitleColor(UIColor.init(hexColor: "f7c201"), for: .normal)
        self.weekButton.addTarget(self, action: #selector(filterButtonClick(sender:)), for: .touchUpInside)
        filterView.addSubview(self.weekButton)
        
        self.filterButtonClick(sender: self.monthButton)
        PKhud.show()
        
        let parameter:postParameters = ["startTime":Date().getExpectTimestamp(year: nil, month: -1, day: nil).startOfCurrentMonth().dateNowAsString("YYYY-MM-dd"),"endTime":Date().getExpectTimestamp(year: nil, month: -1, day: nil).startOfCurrentMonth().endOfCurrentMonth().dateNowAsString("YYYY-MM-dd")]
        self.getOrderStatis(parameter: parameter, isAgain: true)
    }
    
    
    func initHeaderView(){
        let header = UIView.init(frame: CGRect.init(x: 0, y: 0, width: UIScreen.ScreenWidth, height: 60))
        let bgView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: UIScreen.ScreenWidth, height: 55))
        bgView.backgroundColor = UIColor.white
        headerLabel = Utils.labelWith(frame: CGRect.init(x: 0, y: 0, width: UIScreen.ScreenWidth, height: bgView.height), font: UIFont.systemFont(ofSize: 13), textAlignment: NSTextAlignment.center, textColor: "000000", text: "")
        bgView.addSubview(headerLabel)
        header.addSubview(bgView)
        self.tableView.tableHeaderView = header
    }
    
    func updateHeaderLabel(){
        var value = "月"
        if self.type == 1 {
            value = "周"
        }
        let str = "本店本" + value + "共采购\(self.currentModel.count)类商品，累计采购金额\(self.currentModel.money)元"
        var attribateString = NSMutableAttributedString.init(string: str)
        attribateString.addAttributes([NSAttributedStringKey.foregroundColor:UIColor.init(hexColor: "#f7c201")], range: NSString.init(string: str).range(of: "\(self.currentModel.count)"))
        attribateString = NSMutableAttributedString.init(attributedString: attribateString)
        attribateString.addAttributes([NSAttributedStringKey.foregroundColor:UIColor.init(hexColor: "#ed4848")], range: NSString.init(string: str).range(of: "\(self.currentModel.money)元"))
        headerLabel.attributedText = attribateString
    }
    
    func getOrderStatis(parameter:postParameters,isAgain:Bool){
        
        if isAgain == false {
            self.startTime = parameter["startTime"] as! String
            self.endTime = parameter["endTime"] as! String
        }
        
        HttpRequest.requestData(.post, URLString: Api.orderStatis.rawValue, parameters:parameter) { (result) in
            PKhud.hiden()
            if result["msg"] as! String == "success"{
                if let model = JSONDeserializer<OrderStatisModel>.deserializeFrom(dict: result["data"] as? postParameters) {
                    if isAgain{
                        self.lastModel = model
                        if self.type == 0 {
                            let para:postParameters = ["startTime":Date().startOfCurrentMonth().dateNowAsString("YYYY-MM-dd"),"endTime":Date().dateNowAsString("YYYY-MM-dd")]
                            self.getOrderStatis(parameter: para, isAgain: false)
                        }else{
                            let sTime = Date().startOfWeek.dateNowAsString("YYYY-MM-dd")
                            let eTime = Date().dateNowAsString("YYYY-MM-dd")
                            let para:postParameters = ["startTime":sTime,"endTime":eTime]
                            self.getOrderStatis(parameter: para, isAgain: false)
                        }
                        
                    }else{
                        self.currentModel = model
                        self.updateHeaderLabel()
                        self.tableView.reloadData()
                    }
                }
            }else{
                PKhud.showText(text: result["data"] as! String)
            }
        }
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.setNavigationBarHidden(true, animated: true)
    }
    
    @objc func filterButtonClick(sender:UIButton){
        sender.isSelected = true
        sender.backgroundColor = UIColor.init(hexColor: "f7c201")
        if sender.tag == 101 {
            // 本月
            self.type = 0
            self.weekButton.isSelected = false
            self.weekButton.backgroundColor = UIColor.init(hexColor: "ffffff")
            
            let parameter:postParameters = ["startTime":Date().getExpectTimestamp(year: nil, month: -1, day: nil).startOfCurrentMonth().dateNowAsString("YYYY-MM-dd"),"endTime":Date().getExpectTimestamp(year: nil, month: -1, day: nil).startOfCurrentMonth().endOfCurrentMonth().dateNowAsString("YYYY-MM-dd")]
            self.getOrderStatis(parameter: parameter, isAgain: true)
            
        }else{
            // 本周
            self.type = 1
            self.monthButton.isSelected = false
            self.monthButton.backgroundColor = UIColor.init(hexColor: "ffffff")
            let parameter:postParameters = [
                "startTime":Date().getExpectTimestamp(year: nil, month: nil, day: -7).startOfWeek.dateNowAsString("YYYY-MM-dd"),
                "endTime":Date().getExpectTimestamp(year: nil, month: nil, day: -7).endOfWeek.dateNowAsString("YYYY-MM-dd")
            ]
            self.getOrderStatis(parameter: parameter, isAgain: true)
        }
        self.tableView.setContentOffset(CGPoint.init(x: 0, y: 0), animated: false)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    // MARK:返回
    @IBAction func backAction(_ sender: UIButton) {
        self.backBarButtonMethon()
    }
    
    // MARK:右边按钮事件
    @IBAction func filterAction(_ sender: UIButton) {
        if sender.isSelected {
            UIView.animate(withDuration: 0.3, animations: {
                self.filterView.height = 0
            }) { (_) in
                self.effect.alpha = 0
            }
        }else{
            UIView.animate(withDuration: 0.3, animations: {
                self.effect.alpha = 1
                self.filterView.height = 130
            }) { (isFinished) in
            }
        }
        sender.isSelected = !sender.isSelected
    }
    
}


extension EarningsStatementTableViewController:UITableViewDelegate,UITableViewDataSource{
    func numberOfSections(in tableView: UITableView) -> Int {
        var count = 0
        if self.currentModel != nil {
            
            if self.currentModel.fruits != nil || self.currentModel.fresh != nil || self.currentModel.biao != nil {
                count = 2
                if self.currentModel.fruits.gList.count != 0 {
                    count = count + 1
                }
                
                if self.currentModel.fresh.sList.count != 0 {
                    count = count + 1
                }
                if self.currentModel.biao.bList.count != 0 {
                    count = count + 1
                }
            }
        }
        return count
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if self.currentModel == nil {
            return 0
        }
        if section < 2 {
            return 1
        }
        
        switch section {
        case 2:
            if self.currentModel.biao != nil {
                return self.currentModel == nil ? 0 : self.currentModel.biao.bList.count + (self.isScreenHot ? 1 : 0)
            }else if self.currentModel.fruits != nil{
                return self.currentModel == nil ? 0 : self.currentModel.fruits.gList.count + (self.isScreenHot ? 1 : 0)
            }else{
                return self.currentModel == nil ? 0 : self.currentModel.fresh.sList.count + (self.isScreenHot ? 1 : 0)
            }
        case 3:
            if self.currentModel.fruits != nil{
                return self.currentModel == nil ? 0 : self.currentModel.fruits.gList.count + (self.isScreenHot ? 1 : 0)
            }else{
                return self.currentModel == nil ? 0 : self.currentModel.fresh.sList.count + (self.isScreenHot ? 1 : 0)
            }
        case 4:
            return self.currentModel == nil ? 0 : self.currentModel.fresh.sList.count + (self.isScreenHot ? 1 : 0)
            
        default:
            return 0
        }
        
    }
    
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        switch indexPath.section {
        case 0:
            let cell = tableView.dequeueReusableCell(withIdentifier: "PieChartTableViewCell") as! PieChartTableViewCell
            cell.reloadWith(model: self.currentModel, type: self.type)
            return cell
        case 1:
            let cell = tableView.dequeueReusableCell(withIdentifier: "BarGraphTableViewCell") as! BarGraphTableViewCell
            cell.reloadWith(wwithLastModel: self.lastModel, model: self.currentModel,type:self.type)
            return cell
        default:
            if self.isScreenHot == false {
                let cell = tableView.dequeueReusableCell(withIdentifier: "StatisticsGoodsTableViewCell") as! StatisticsGoodsTableViewCell
                
                switch indexPath.section {
                case 2:
                    if self.currentModel.biao != nil {
                        cell.reload(WithModel: self.currentModel.biao.bList[indexPath.row])
                    }else if self.currentModel.fruits != nil{
                        cell.reload(WithModel: self.currentModel.fruits.gList[indexPath.row])
                    }else{
                        cell.reload(WithModel: self.currentModel.fresh.sList[indexPath.row])
                    }
                case 3:
                    if self.currentModel.fruits != nil{
                        cell.reload(WithModel: self.currentModel.fruits.gList[indexPath.row])
                    }else{
                        cell.reload(WithModel: self.currentModel.fresh.sList[indexPath.row])
                    }
                case 4:
                    cell.reload(WithModel: self.currentModel.fresh.sList[indexPath.row])
                    
                default:
                    break
                }
                
                if indexPath.row == 3 {
                    cell.viewLine.isHidden = true
                }else{
                    cell.viewLine.isHidden = false
                }
                return cell
            }else{
                let cell = tableView.dequeueReusableCell(withIdentifier: "OrderStatisticsTableViewCell") as! OrderStatisticsTableViewCell
                if indexPath.row != 0 {
                    switch indexPath.section {
                    case 2:
                        if self.currentModel.biao.bCount != 0 {
                            cell.reload(withName: (self.currentModel.biao.bList[indexPath.row-1].goods?.name)!, count: "\(self.currentModel.biao.bCount)"+(self.currentModel.biao.bList[indexPath.row-1].goods?.spec)!, money: "\(self.currentModel.biao.bList[indexPath.row-1].price)")
                        }else if self.currentModel.fruits.gCount != 0{
                            cell.reload(withName: (self.currentModel.fruits.gList[indexPath.row-1].goods?.name)!, count: "\(self.currentModel.fruits.gCount)"+(self.currentModel.fruits.gList[indexPath.row-1].goods?.spec)!, money: "\(self.currentModel.fruits.gList[indexPath.row-1].price)")
                        }else{
                            cell.reload(withName: (self.currentModel.fresh.sList[indexPath.row-1].goods?.name)!, count: "\(self.currentModel.fresh.sCount)"+(self.currentModel.fresh.sList[indexPath.row-1].goods?.spec)!, money: "\(self.currentModel.fresh.sList[indexPath.row-1].price)")
                        }
                    case 3:
                        if self.currentModel.fruits.gCount != 0{
                            cell.reload(withName: (self.currentModel.fruits.gList[indexPath.row-1].goods?.name)!, count: "\(self.currentModel.fruits.gCount)"+(self.currentModel.fruits.gList[indexPath.row-1].goods?.spec)!, money: "\(self.currentModel.fruits.gList[indexPath.row-1].price)")
                        }else{
                            cell.reload(withName: (self.currentModel.fresh.sList[indexPath.row-1].goods?.name)!, count: "\(self.currentModel.fresh.sCount)"+(self.currentModel.fresh.sList[indexPath.row-1].goods?.spec)!, money: "\(self.currentModel.fresh.sList[indexPath.row-1].price)")
                        }
                    case 4:
                        cell.reload(withName: (self.currentModel.fresh.sList[indexPath.row-1].goods?.name)!, count: "\(self.currentModel.fresh.sCount)"+(self.currentModel.fresh.sList[indexPath.row-1].goods?.spec)!, money: "\(self.currentModel.fresh.sList[indexPath.row-1].price)")
                        
                    default:
                        break
                    }
                }else{
                    cell.reload(withName:"品名", count: "累计采购量", money: "合计金额")
                }
                return cell
            }
        }
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        var id = 0
        var img = ""
        var name = ""
        switch indexPath.section {
        case 2:
            if self.currentModel.biao.bCount != 0 {
                id = (self.currentModel.biao.bList[indexPath.row].goods?.id)!
                name = (self.currentModel.biao.bList[indexPath.row].goods?.name)!
                img = (self.currentModel.biao.bList[indexPath.row].goods?.img)!
            }else if self.currentModel.fruits.gCount != 0{
                id = (self.currentModel.fruits.gList[indexPath.row].goods?.id)!
                name = (self.currentModel.fruits.gList[indexPath.row].goods?.name)!
                img = (self.currentModel.fruits.gList[indexPath.row].goods?.img)!
            }else{
                id = (self.currentModel.fresh.sList[indexPath.row].goods?.id)!
                name = (self.currentModel.fresh.sList[indexPath.row].goods?.name)!
                img = (self.currentModel.fresh.sList[indexPath.row].goods?.img)!
            }
        case 3:
            if self.currentModel.fruits.gCount != 0{
                id = (self.currentModel.fruits.gList[indexPath.row].goods?.id)!
                name = (self.currentModel.fruits.gList[indexPath.row].goods?.name)!
                img = (self.currentModel.fruits.gList[indexPath.row].goods?.img)!
            }else{
                id = (self.currentModel.fresh.sList[indexPath.row].goods?.id)!
                name = (self.currentModel.fresh.sList[indexPath.row].goods?.name)!
                img = (self.currentModel.fresh.sList[indexPath.row].goods?.img)!
            }
        case 4:
            id = (self.currentModel.fresh.sList[indexPath.row].goods?.id)!
            name = (self.currentModel.fresh.sList[indexPath.row].goods?.name)!
            img = (self.currentModel.fresh.sList[indexPath.row].goods?.img)!
        default:
            break
        }
        guard id != 0 else {
            return
        }
        
        let ctrl = Utils.instantiateInitialViewControllerWith(storyBoardName: "Main", identitier: "SingleDetailTableViewController") as! SingleDetailTableViewController
        ctrl.startTime = self.startTime
        ctrl.endTime = self.endTime
        ctrl.id = id
        ctrl.img = img
        ctrl.name = name
        
        self.navigationController?.pushViewController(ctrl, animated: true)
        
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        switch indexPath.section {
        case 0:
            return 280
        case 1:
            return 285
        default:
            return self.isScreenHot ? 44 : 100
        }
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        if section >= 2 {
            let header = UIView.init(frame: CGRect.init(x: 0, y: 0, width: UIScreen.ScreenWidth, height: 48 - 15))
            header.backgroundColor = UIColor.nav_color
            
            var title = ""
            var count = 0
            var money:Double = 0
            switch section {
            case 2:
                if self.currentModel.biao.bCount != 0 {
                    title = "标类：累计采购\(self.currentModel.biao.bCount)" + "种商品，" + "合计\(self.currentModel.biao.bMoney)元"
                    count = self.currentModel.biao.bCount
                    money = self.currentModel.biao.bMoney
                }else if self.currentModel.fruits.gCount != 0{
                    title = "果蔬类：累计采购\(self.currentModel.fruits.gCount)" + "种商品，" + "合计\(self.currentModel.fruits.gMoney)元"
                    count = self.currentModel.fruits.gCount
                    money = self.currentModel.fruits.gMoney
                }else{
                    title = "生鲜类：累计采购\(self.currentModel.fresh.sCount)" + "种商品，" + "合计\(self.currentModel.fresh.sMoney)元"
                    count = self.currentModel.fresh.sCount
                    money = self.currentModel.fresh.sMoney
                }
            case 3:
                if self.currentModel.fruits.gCount != 0{
                    title = "果蔬类：累计采购\(self.currentModel.fruits.gCount)" + "种商品，" + "合计\(self.currentModel.fruits.gMoney)元"
                    count = self.currentModel.fruits.gCount
                    money = self.currentModel.fruits.gMoney
                }else{
                    title = "生鲜类：累计采购\(self.currentModel.fresh.sCount)" + "种商品，" + "合计\(self.currentModel.fresh.sMoney)元"
                    count = self.currentModel.fresh.sCount
                    money = self.currentModel.fresh.sMoney
                }
            case 4:
                title = "生鲜类：累计采购\(self.currentModel.fresh.sCount)" + "种商品，" + "合计\(self.currentModel.fresh.sMoney)元"
                count = self.currentModel.fresh.sCount
                money = self.currentModel.fresh.sMoney
            default:
                break
            }
            
            let titleLabel = Utils.labelWith(frame: CGRect.init(x: 12, y: 10, width: UIScreen.ScreenWidth - 24, height: 15), font: UIFont.boldSystemFont(ofSize: 14), textAlignment: .left, textColor: "#000000", text: "")
            header.addSubview(titleLabel)
            
            var attribateString = NSMutableAttributedString.init(string: title)
            attribateString.addAttributes([NSAttributedStringKey.foregroundColor:UIColor.init(hexColor: "#f7c201")], range: NSString.init(string: title).range(of: "\(count)"))
            attribateString = NSMutableAttributedString.init(attributedString: attribateString)
            attribateString.addAttributes([NSAttributedStringKey.foregroundColor:UIColor.init(hexColor: "#ed4848")], range: NSString.init(string: title).range(of: "\(money)元"))
            titleLabel.attributedText = attribateString
            
            return header
        }
        return UIView()
    }
    
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if section == 0 || section == 1 {
            return 5
        }
        if section == 2 {
            return 43 - 15
        }
        if section > 2 {
            return 38 - 10
        }
        return 0.01
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        if section == 1 {
            return 15
        }
        if section > 1 {
            return 10
        }
        return 0.01
    }
    
    
}
